

/**************************************************START OF FILE*****************************************************/





/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "libusb/libusb.h"
#include <QDebug>
#include "x_hiddevice.h"
#include "keeloq.h"
#include "Tool.h"
#include "QRegExpValidator"
#include "QTimer"




/*------------------------------------------------------------------------------------------------------------------
Var
*/
uint16_t HCS301_ConfigValue[12]=
{
    0xCDEF,		//key_0
    0x89AB, 	//key_1
    0x4567, 	//key_2
    0x0123,		//key_3
    0x0000,		//SYNC
    0x0000,		//RES
    0x0000,		//SER_0 序列号
    0x8000,		//SER_1	序列号
    0x0000, 	//SEED_0 种子
    0x0000,		//SEED_1 种子
    0x0000, 	//RES
    0x1137,		//CONFIG    //11
};

//脱机烧录时的四个配置信息
uint16_t HCS301_ConfigValue_Config[4]=
 {
    0x0000,		//模式0：简单模式 1:正常模式 2：安全模式 //12
    0x0000,		//种子值自动加一                     //13
    0x0000,		//序列号自动加一                     //14
    0x0000,		//识别码取自序列号                   //15
 };


//实际加密的密钥
uint32_t keyt[2];

//标记学习模式
uint32_t LearnModel = 0;


//标记四个复选模式状态
bool isSeedAdd = false;
bool isSerialAdd = false;
bool isIdFromSer = false;
bool isShowPassWord = false;




/*
*********************************************************************************************************************

@ Brief  : 构造函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-23 10:32:24

*********************************************************************************************************************
*/
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //加载要用到的图片
    img_led_r = new QImage(":/img/img/LED_R.png");
    img_led_g = new QImage(":/img/img/LED_G.png");
    img_led_y = new QImage(":/img/img/LED_Y.png");

    //UI设置显示图片
    ui->Image_Connect->setPixmap(QPixmap::fromImage(*img_led_r));
    ui->Image_Oparation->setPixmap(QPixmap::fromImage(*img_led_r));
    ui->Image_Connect->setScaledContents(true);
    ui->Image_Oparation->setScaledContents(true);
    ui->LB_FluseStatus->setText("烧录失败");
    ui->LB_ConnectStatus->setText("设备已断开");

    //设置输入模式的表达式
    //厂商代码 64bit
    QRegExp product_rx("[0-9A-Fa-f]{0,16}");
    QRegExpValidator *validator = new QRegExpValidator(product_rx, this);
    ui->ProductCode->setValidator(validator);

    //序列号 32bit
    QRegExp SerialNumber_rx("[0-9A-Fa-f]{0,7}");
    validator = new QRegExpValidator(SerialNumber_rx, this);
    ui->SerialNumber->setValidator(validator);

    //同步值16bit
    QRegExp SyncCount_rx("[0-9A-Fa-f]{0,4}");
    validator = new QRegExpValidator(SyncCount_rx, this);
    ui->SyncCount->setValidator(validator);

    //识别码10bit
    QRegExp IdCode_rx("[0-9A-Fa-f]{0,3}");
    validator = new QRegExpValidator(IdCode_rx, this);
    ui->IdCode->setValidator(validator);

    //种子值 32bit
    QRegExp SeedValue_rx("[0-9A-Fa-f]{0,8}");
    validator = new QRegExpValidator(SeedValue_rx, this);
    ui->SeedValue->setValidator(validator);


    //学习模式分组
    btnGroupLearn = new QButtonGroup(this);
    btnGroupLearn->addButton(ui->radioButton0, 0);
    btnGroupLearn->addButton(ui->radioButton1, 1);
    btnGroupLearn->addButton(ui->radioButton2, 2);
    ui->radioButton0->setChecked(true);
    connect(ui->radioButton0, SIGNAL(clicked()), this, SLOT(onRadioClickLearn()));
    connect(ui->radioButton1, SIGNAL(clicked()), this, SLOT(onRadioClickLearn()));
    connect(ui->radioButton2, SIGNAL(clicked()), this, SLOT(onRadioClickLearn()));


    //波特率分组
    btnGroupBaudRate = new QButtonGroup(this);
    btnGroupBaudRate->addButton(ui->radioButton_4, 0);
    btnGroupBaudRate->addButton(ui->radioButton_5, 1);
    btnGroupBaudRate->addButton(ui->radioButton_6, 2);
    btnGroupBaudRate->addButton(ui->radioButton_7, 3);
    ui->radioButton_4->setChecked(true);

    connect(ui->radioButton_4, SIGNAL(clicked()), this, SLOT(onRadioClickBaudRate()));
    connect(ui->radioButton_5, SIGNAL(clicked()), this, SLOT(onRadioClickBaudRate()));
    connect(ui->radioButton_6, SIGNAL(clicked()), this, SLOT(onRadioClickBaudRate()));
    connect(ui->radioButton_7, SIGNAL(clicked()), this, SLOT(onRadioClickBaudRate()));

    //波特率分组
    btnGroupVoltageChk = new QButtonGroup(this);
    btnGroupVoltageChk->addButton(ui->radioButton_8, 0);
    btnGroupVoltageChk->addButton(ui->radioButton_9, 1);
    ui->radioButton_8->setChecked(true);
    connect(ui->radioButton_8, SIGNAL(clicked()), this, SLOT(onRadioClickVoltageChk()));
    connect(ui->radioButton_9, SIGNAL(clicked()), this, SLOT(onRadioClickVoltageChk()));


    //定时器分组
    btnGroupTimer = new QButtonGroup(this);
    btnGroupTimer->addButton(ui->radioButton_10, 0);
    btnGroupTimer->addButton(ui->radioButton_11, 1);
    ui->radioButton_10->setChecked(true);
    connect(ui->radioButton_10, SIGNAL(clicked()), this, SLOT(onRadioClickTimer()));
    connect(ui->radioButton_11, SIGNAL(clicked()), this, SLOT(onRadioClickTimer()));


    btnGroupOverFlow = new QButtonGroup(this);
    btnGroupOverFlow->addButton(ui->radioButton13, 0);
    btnGroupOverFlow->addButton(ui->radioButton14, 1);
    btnGroupOverFlow->addButton(ui->radioButton15, 2);
    btnGroupOverFlow->addButton(ui->radioButton16, 3);
    ui->radioButton13->setChecked(true);
    connect(ui->radioButton13, SIGNAL(clicked()), this, SLOT(onRadioClickOverFlow()));
    connect(ui->radioButton14, SIGNAL(clicked()), this, SLOT(onRadioClickOverFlow()));
    connect(ui->radioButton15, SIGNAL(clicked()), this, SLOT(onRadioClickOverFlow()));
    connect(ui->radioButton16, SIGNAL(clicked()), this, SLOT(onRadioClickOverFlow()));


    CK_UsbDevTim = new QTimer(this);
    QObject::connect(CK_UsbDevTim , &QTimer::timeout,this,&MainWindow::Timeout_handle);
    CK_UsbDevTim->start(500);

    ReadDataTim = new QTimer(this);
    QObject::connect(ReadDataTim , &QTimer::timeout,this,&MainWindow::ReadData_handle);


    X_HidDevice::LibInit();

    HcsxxxDevice = new X_HidDevice();


}
/*
*********************************************************************************************************************

@ Brief  : 析构函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:40:00

*********************************************************************************************************************
*/
MainWindow::~MainWindow()
{
    delete img_led_r;
    delete img_led_g;
    delete img_led_y;

    delete  HcsxxxDevice;
    X_HidDevice::LibExit();
    delete ui;
}



/*
*********************************************************************************************************************

@ Brief  : 下载数据到芯片

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-20 16:35:40

*********************************************************************************************************************
*/
void MainWindow::on_Button_Download_clicked()
{
    uint32_t SerialNumber;      //序列号
    uint8_t Key[8];             //密钥
    uint8_t Source[4];          //加解密数据

    if(HcsxxxDevice->isDeviceOpen == false)
    {
        return;
    }

    ui->LB_FluseStatus->setText("正在写入...");
    ui->Image_Oparation->setPixmap(QPixmap::fromImage(*img_led_y));

    switch(LearnModel)
    {
    //简单
    case 0:
    {
        keyt[1] = HCS301_ConfigValue[3] | (HCS301_ConfigValue[2]<<16);
        keyt[0] = HCS301_ConfigValue[1] | (HCS301_ConfigValue[0]<<16);

        uint32_t ShowBuffer[2];

        ShowBuffer[1] = HCS301_ConfigValue[0] | (HCS301_ConfigValue[1]<<16);
        ShowBuffer[0] = HCS301_ConfigValue[2] | (HCS301_ConfigValue[3]<<16);

        QString str = QString("%1%2").arg(ShowBuffer[0],8,16,QLatin1Char('0')).arg(ShowBuffer[1],8,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->SecretKey->setText(str);
    }

        break;
    //标准
    case 1:
    {
        uint32_t Reslut;
        SerialNumber = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        SerialNumber &= 0x0FFFFFFF;
        for (uint8_t i = 0; i< 4; i++)
        {
            Key[2*i] = HCS301_ConfigValue[i];
            Key[2*i+1] = HCS301_ConfigValue[i] >> 8;
        }

        uint32_t Temp = SerialNumber | 0x60000000;
        for (uint8_t i = 0; i< 4; i++)
        {
            Source[i] = (Temp >> (i*8));
        }
        KEELOQ_Decrypt(Source , Key);
        Reslut =TOOL_Uint8ToUint32_H(Source);
        keyt[0] = Reslut;


        Temp = SerialNumber | 0x20000000;
        for (uint8_t i = 0; i< 4; i++)
        {
            Source[i] = (Temp >> (i*8));
        }

        KEELOQ_Decrypt(Source , Key);
        Reslut =TOOL_Uint8ToUint32_H(Source);
        keyt[1] = Reslut;

         //显示到输出框
        QString str = QString("%1%2").arg(keyt[0],8,16,QLatin1Char('0')).arg(keyt[1],8,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->SecretKey->setText(str);

		//显示与写入的格式不一样
		uint8_t u8Temp;
		Temp = keyt[0];
		TOOL_Uint32ToUint8_H(Source , keyt[1]);
		u8Temp = Source[0];
		Source[0] = Source[1];
		Source[1] = u8Temp;
		u8Temp = Source[2];
		Source[2] = Source[3];
		Source[3] = u8Temp;
		keyt[0] = TOOL_Uint8ToUint32_L(Source);
		
		TOOL_Uint32ToUint8_H(Source , Temp);
		u8Temp = Source[0];
		Source[0] = Source[1];
		Source[1] = u8Temp;
		u8Temp = Source[2];
		Source[2] = Source[3];
		Source[3] = u8Temp;
		keyt[1] = TOOL_Uint8ToUint32_L(Source);


    }
    break;
    //安全
    case 2:
    {
        uint32_t Reslut;
        SerialNumber = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        SerialNumber &= 0x0FFFFFFF;
        for (uint8_t i = 0; i< 4; i++)
        {
            Key[2*i] = HCS301_ConfigValue[i];
            Key[2*i+1] = HCS301_ConfigValue[i] >> 8;
        }
        uint32_t Temp = SerialNumber;
        for (uint8_t i = 0; i< 4; i++)
        {
            Source[i] = (Temp >> (i*8));
        }

        KEELOQ_Decrypt(Source , Key);

        Reslut =TOOL_Uint8ToUint32_H(Source);
        keyt[0] = Reslut;

        SerialNumber = HCS301_ConfigValue[8] | (HCS301_ConfigValue[9]<<16);
        Temp = SerialNumber;
        for (uint8_t i = 0; i< 4; i++)
        {
            Source[i] = (Temp >> (i*8));
        }

        KEELOQ_Decrypt(Source , Key);
        Reslut =TOOL_Uint8ToUint32_H(Source);
        keyt[1] = Reslut;

        QString str = QString("%1%2").arg(keyt[0],8,16,QLatin1Char('0')).arg(keyt[1],8,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->SecretKey->setText(str);

		//显示与写入的格式不一样
		uint8_t u8Temp;
		Temp = keyt[0];
		TOOL_Uint32ToUint8_H(Source , keyt[1]);
		u8Temp = Source[0];
		Source[0] = Source[1];
		Source[1] = u8Temp;
		u8Temp = Source[2];
		Source[2] = Source[3];
		Source[3] = u8Temp;
		keyt[0] = TOOL_Uint8ToUint32_L(Source);
		
		TOOL_Uint32ToUint8_H(Source , Temp);
		u8Temp = Source[0];
		Source[0] = Source[1];
		Source[1] = u8Temp;
		u8Temp = Source[2];
		Source[2] = Source[3];
		Source[3] = u8Temp;
		keyt[1] = TOOL_Uint8ToUint32_L(Source);
    }
        break;
    }

    //向USB写入命令
    WriteHcs301();

    //启动定时器
    ReadDataTim->start(500);


    //序列号自动加一
    if(isSerialAdd == true)
    {
        uint32_t bit31 = 0;
        SerialNumber = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        bit31 = SerialNumber & 0x80000000;
        SerialNumber &= 0x0FFFFFFF;
        SerialNumber++;
        SerialNumber &= 0x0FFFFFFF;
        HCS301_ConfigValue[6] = (SerialNumber | bit31);
        HCS301_ConfigValue[7] = (SerialNumber | bit31)>>16;
        //回显到界面
        QString str = QString("%1").arg(SerialNumber,8,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->SerialNumber->setText(str);
    }

    //种子值自动加一
    if(isSeedAdd == true)
    {
        SerialNumber = HCS301_ConfigValue[8] | (HCS301_ConfigValue[9]<<16);
        SerialNumber++;
        HCS301_ConfigValue[8] = SerialNumber;
        HCS301_ConfigValue[9] = SerialNumber>>16;
        //回显到界面
        QString str = QString("%1").arg(SerialNumber,8,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->SeedValue->setText(str);
    }

    //识别码更新
    if(isIdFromSer == true)
    {
        uint32_t TempData;
        TempData = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        TempData &= 0x3FF;
        //写入到数组中
        HCS301_ConfigValue[11] &= 0xFC00;  		//低10位清零
        HCS301_ConfigValue[11] |= TempData;   	//改变值
        //回显到界面
        QString str = QString("%1").arg(TempData,3,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->IdCode->setText(str);
    }

}



/*
*********************************************************************************************************************

@ Brief  :

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:09:55

*********************************************************************************************************************
*/
void MainWindow::Timeout_handle(void)
{
    libusb_device **devs;
    int cnt;

    if(HcsxxxDevice->isDeviceOpen == false)
    {
        HcsxxxDevice->OpenDevice(0x572C , 0x0483);
    }
    else
    {
        cnt = libusb_get_device_list(NULL, &devs);
        if (cnt < 0)
        {
            HcsxxxDevice->isDeviceOpen = false;
            HcsxxxDevice->Close();
        }
        else
        {
            bool isFindDevice = false;
            uint8_t index=0;
            for (; index < cnt; index++)
            {
                if(HcsxxxDevice->Device_Addr == libusb_get_device_address(devs[index]))
                {
                    isFindDevice = true;
                    break;
                }
            }
            //打不到对应的设备地址
            if(isFindDevice == false)
            {
                HcsxxxDevice->isDeviceOpen = false;
                HcsxxxDevice->Close();
            }
        }

        libusb_free_device_list(devs, 1);
    }

    if(HcsxxxDevice->isDeviceOpen == false)
    {
        ui->LB_ConnectStatus->setText("设备已断开");
        ui->Image_Connect->setPixmap(QPixmap::fromImage(*img_led_r));
    }
    else
    {

        ui->LB_ConnectStatus->setText("设备已连接");
        ui->Image_Connect->setPixmap(QPixmap::fromImage(*img_led_g));
    }

}

/*
*********************************************************************************************************************

@ Brief  : 接收到的数据处理函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:10:04

*********************************************************************************************************************
*/
void MainWindow::ReadData_handle(void)
{
    uint8_t RxBuffer[64];
    HcsxxxDevice->ReadData(RxBuffer);

    if(RxBuffer[2] == 0xE1)
    {
        if(RxBuffer[3])
        {
            ui->LB_FluseStatus->setText("烧录成功");
            ui->Image_Oparation->setPixmap(QPixmap::fromImage(*img_led_g));
        }
        else
        {
            ui->LB_FluseStatus->setText("烧录失败");
            ui->Image_Oparation->setPixmap(QPixmap::fromImage(*img_led_r));
        }
    }
    ReadDataTim->stop();

}
/*
*********************************************************************************************************************

@ Brief  : 脱机下载按键

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-20 16:36:03

*********************************************************************************************************************
*/
void MainWindow::on_Button_Updata_clicked()
{
    //设备没有打开
    if(HcsxxxDevice->isDeviceOpen == false)
    {
        return;
    }

    ui->LB_FluseStatus->setText("写入配置中...");
    WriteHcs301_Config();
    ui->LB_FluseStatus->setText("写入配置成功");
    ui->Image_Oparation->setPixmap(QPixmap::fromImage(*img_led_g));
}



/*
*********************************************************************************************************************

@ Brief  : 厂商代码改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:16:27

*********************************************************************************************************************
*/
void MainWindow::on_ProductCode_textChanged(const QString &arg1)
{

    uint64_t ReturnData;
    bool ok;
    ReturnData = arg1.toULongLong(&ok, 16);
    qDebug() << arg1 << ReturnData;
    qDebug("%llx" , ReturnData);

    HCS301_ConfigValue[0] = ReturnData >> 0;
    HCS301_ConfigValue[1] = ReturnData >> 16;
    HCS301_ConfigValue[2] = ReturnData >> 32;
    HCS301_ConfigValue[3] = ReturnData >> 48;

}
/*
*********************************************************************************************************************

@ Brief  : 序列号改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:16:54

*********************************************************************************************************************
*/
void MainWindow::on_SerialNumber_textChanged(const QString &arg1)
{

    uint32_t ReturnData;
    bool bit31;

    bool ok;
    ReturnData = arg1.toUInt(&ok, 16);

    HCS301_ConfigValue[6] = ReturnData >> 0;

    if(HCS301_ConfigValue[7] & 0x8000)
    {
        bit31 = true;
    }
    else
    {
        bit31 = false;
    }

    HCS301_ConfigValue[7] = ReturnData >> 16;

    if(bit31)
    {
        HCS301_ConfigValue[7] |= 0x8000;
    }
    else
    {
        HCS301_ConfigValue[7] &= (~0x8000);
    }

     //更新识别码
    if(isIdFromSer == true)
    {
        uint32_t TempData;
        TempData = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        TempData &= 0x3FF;
        //回显到界面
        QString str = QString("%1").arg(TempData,3,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->IdCode->setText(str);
    }


}

/*
*********************************************************************************************************************

@ Brief  : 同步计数值改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:17:28

*********************************************************************************************************************
*/
void MainWindow::on_SyncCount_textChanged(const QString &arg1)
{
    uint16_t ReturnData;
    bool ok;
    ReturnData = arg1.toUShort(&ok, 16);

    HCS301_ConfigValue[4] = ReturnData;


}

/*
*********************************************************************************************************************

@ Brief  : 识别码改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:17:51

*********************************************************************************************************************
*/
void MainWindow::on_IdCode_textChanged(const QString &arg1)
{
    uint16_t ReturnData;
    bool ok;
    ReturnData = arg1.toUShort(&ok, 16);

    HCS301_ConfigValue[11] &= 0xFC00;       //低10位清零
    ReturnData &= 0x03FF;                   //高位清零
    HCS301_ConfigValue[11] |= ReturnData;   //改变值

}

/*
*********************************************************************************************************************

@ Brief  : 种子值改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:18:20

*********************************************************************************************************************
*/
void MainWindow::on_SeedValue_textChanged(const QString &arg1)
{
    uint32_t ReturnData;

    bool ok;
    ReturnData = arg1.toUInt(&ok, 16);

    HCS301_ConfigValue[8] = ReturnData >> 0;
    HCS301_ConfigValue[9] = ReturnData >> 16;
}


/*
*********************************************************************************************************************

@ Brief  : 学习模式选择事件

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-21 16:24:59

*********************************************************************************************************************
*/
void MainWindow::onRadioClickLearn()
{
    switch(btnGroupLearn->checkedId())
    {
    case 0:
        LearnModel = 0;
        break;
    case 1:
        LearnModel = 1;
        break;
    case 2:
        LearnModel = 2;
        break;
    }
}

/*
*********************************************************************************************************************

@ Brief  : 波特率设置

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-21 16:25:35

*********************************************************************************************************************
*/
void MainWindow::onRadioClickBaudRate()
{
    switch(btnGroupBaudRate->checkedId())
    {
    case 0:
        HCS301_ConfigValue[11] &= (~(3U<<13));
        HCS301_ConfigValue[11] |= (0U<<13);
        break;
    case 1:
        HCS301_ConfigValue[11] &= (~(3U<<13));
        HCS301_ConfigValue[11] |= (1U<<13);
        break;
    case 2:
        HCS301_ConfigValue[11] &= (~(3U<<13));
        HCS301_ConfigValue[11] |= (2U<<13);
        break;
    case 3:
        HCS301_ConfigValue[11] &= (~(3U<<13));
        HCS301_ConfigValue[11] |= (3U<<13);
        break;
    }
}
/*
*********************************************************************************************************************

@ Brief  : 电压检测设置

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-21 16:25:55

*********************************************************************************************************************
*/
void MainWindow::onRadioClickVoltageChk()
{
    switch(btnGroupVoltageChk->checkedId())
    {
    case 0:
        //设置为1
        HCS301_ConfigValue[11] |= (1U<<12);
        break;
    case 1:
        //设置为0
        HCS301_ConfigValue[11] &= (~(1U<<12));
        break;
    }
}
/*
*********************************************************************************************************************

@ Brief  : 定时器开关设置

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-21 16:26:11

*********************************************************************************************************************
*/
void MainWindow::onRadioClickTimer()
{
    switch(btnGroupTimer->checkedId())
    {
    case 0:
        HCS301_ConfigValue[7] |= 0x8000;
        break;
    case 1:
        HCS301_ConfigValue[7] &= (~0x8000);
        break;
    }
}

/*
*********************************************************************************************************************

@ Brief  : 溢出位选择的改变

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 13:50:42

*********************************************************************************************************************
*/
void MainWindow::onRadioClickOverFlow()
{
    switch(btnGroupOverFlow->checkedId())
    {
    case 0:
        HCS301_ConfigValue[11] &= (~(3U<<10));
        HCS301_ConfigValue[11] |= (0U<<10);
        break;
    case 1:
        HCS301_ConfigValue[11] &= (~(3U<<10));
        HCS301_ConfigValue[11] |= (1U<<10);
        break;
    case 2:
        HCS301_ConfigValue[11] &= (~(3U<<10));
        HCS301_ConfigValue[11] |= (2U<<10);
        break;
    case 3:
        HCS301_ConfigValue[11] &= (~(3U<<10));
        HCS301_ConfigValue[11] |= (3U<<10);
        break;
    }

}


/*
*********************************************************************************************************************

@ Brief  : 四个复选框的状态改变信号

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-22 21:39:21

*********************************************************************************************************************
*/
void MainWindow::on_CB_SeedAdd_stateChanged(int arg1)
{
    if(arg1)
    {
        isSeedAdd = true;
    }
    else
    {
        isSeedAdd = false;
    }
}

void MainWindow::on_CB_SerialAdd_stateChanged(int arg1)
{
    if(arg1)
    {
        isSerialAdd = true;
    }
    else
    {
        isSerialAdd = false;
    }
}

void MainWindow::on_CB_IdCodeFromSerial_stateChanged(int arg1)
{
    uint32_t TempData;
    if(arg1)
    {
        isIdFromSer = true;
        TempData = HCS301_ConfigValue[6] | (HCS301_ConfigValue[7]<<16);
        TempData &= 0x3FF;

        //回显到界面
        QString str = QString("%1").arg(TempData,3,16,QLatin1Char('0'));
        str = str.toUpper();
        ui->IdCode->setText(str);
        ui->IdCode->setEnabled(false);
    }
    else
    {
        ui->IdCode->setEnabled(true);
        isIdFromSer = false;
    }
}

void MainWindow::on_CB_ShowPassword_stateChanged(int arg1)
{
    if(arg1)
    {
        isShowPassWord = true;
        ui->SecretKey->setEchoMode(QLineEdit::Password);
    }
    else
    {
        isShowPassWord = false;
        ui->SecretKey->setEchoMode(QLineEdit::Normal);
    }
}



/*
*********************************************************************************************************************

@ Brief  :

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:10:46

*********************************************************************************************************************
*/
void MainWindow::SendCmdToDevice(uint8_t* TxBuffer)
{
        uint16_t CheckSum = 0;
        //设置头跟尾
        TxBuffer[0] = 0xAA;
        TxBuffer[1] = 0x55;
        TxBuffer[62] = 0x55;
        TxBuffer[63] = 0xAA;
        //校验
        for (int index = 0; index < 58; ++index)
        {
            CheckSum += TxBuffer[index + 2];
        }
        TxBuffer[61] = CheckSum;
        TxBuffer[60] = CheckSum>>8;
        HcsxxxDevice->WriteData(TxBuffer);
}

/*
*********************************************************************************************************************

@ Brief  : 打印uint8_t类型数组

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2021-06-13 17:37:50

*********************************************************************************************************************
*/
void xAPP_TOOL_PrintUint8_t(uint8_t* Buffer , uint32_t Len)
{
    QString str;
   for(uint32_t i = 0 ; i < Len ; i++)
   {
       str += ("0x" + QString("%1 ").arg(Buffer[i],2,16,QLatin1Char('0')).toUpper());
    }
    qDebug() << str;
}


/*
*********************************************************************************************************************

@ Brief  :

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:10:37

*********************************************************************************************************************
*/
void MainWindow::WriteHcs301(void)
{

    uint8_t SendBuffer[64];
    SendBuffer[2] = 0xF1;
    for(uint8_t i = 4 ; i < 12 ; i++)
    {
        SendBuffer[3 + 2*i] = HCS301_ConfigValue[i] >> 8;
        SendBuffer[4 + 2*i] = HCS301_ConfigValue[i] >> 0;
    }

    TOOL_Uint32ToUint8_L(&SendBuffer[3] , keyt[0]);
    TOOL_Uint32ToUint8_L(&SendBuffer[7] , keyt[1]);

	xAPP_TOOL_PrintUint8_t(&SendBuffer[3] , 8);

    SendCmdToDevice(SendBuffer);
}


/*
*********************************************************************************************************************

@ Brief  :

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-12-02 14:10:27

*********************************************************************************************************************
*/
void MainWindow::WriteHcs301_Config(void)
{

    uint8_t SendBuffer[64];
    SendBuffer[2] = 0xF2;

    for(uint8_t i = 0 ; i < 12 ; i++)
    {
        SendBuffer[3 + 2 * i] = HCS301_ConfigValue[i] >> 8;
        SendBuffer[4 + 2 * i] = HCS301_ConfigValue[i] >> 0;
    }

    //四种模式的读取
    HCS301_ConfigValue_Config[0] = LearnModel;
    HCS301_ConfigValue_Config[1] = isSeedAdd;
    HCS301_ConfigValue_Config[2] = isSerialAdd;
    HCS301_ConfigValue_Config[3] = isIdFromSer;

    //增加4个配置信息
    for(uint8_t i = 0 ; i < 4 ; i++)
    {
        SendBuffer[27 + 2*i] = HCS301_ConfigValue_Config[i] >> 8;
        SendBuffer[28 + 2*i] = HCS301_ConfigValue_Config[i] >> 0;
    }


    SendCmdToDevice(SendBuffer);
}



/**************************************************END OF FILE**********************************************************/



